﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebStudio.Business.Entity;
using WebStudio.Framework.Helper;
using System.Data;

namespace WebStudio.Business.Rule
{
    public class AlbumRule
    {
        #region 共有方法

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns>列表</returns>
        public static List<AlbumEntity> GetAlbumList(string condition)
        {
            List<AlbumEntity> list = new List<AlbumEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM AlbumTB where {0}",condition)))
            {
                return FillData(dr);
            }
        }

        public static List<AlbumEntity> GetDeepAlbumList(string condition)
        {
            List<AlbumEntity> list = new List<AlbumEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM AlbumTB where {0}", condition)))
            {
                return DeepFillData(dr);
            }
        }

        public static List<AlbumEntity> GetAlbumlList(int pageIndex, int pageSize, int pageCount, int recordCount)
        {
            List<AlbumEntity> list = new List<AlbumEntity>();
            string strSql = string.Empty;
            if (pageIndex >= pageCount)
            {
                if (recordCount == 0)
                {
                    strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + " * FROM AlbumTB order by AlbumId desc) order by AlbumId) order by AlbumId desc";
                }
                else
                {
                    strSql = "select * from (select top " + (recordCount - (pageIndex - 1) * pageSize) + " * from (select top " + pageIndex * pageSize + " * FROM AlbumTB order by AlbumId desc) order by AlbumId) order by AlbumId desc";
                }
            }
            else
            {
                strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + "  * FROM AlbumTB order by AlbumId desc) order by AlbumId) order by AlbumId desc";
            }
            using (var dr = DBHelper.ExecuteReader(strSql))
            {
                return FillData(dr);
            }
        }

        public static int GetAlbumListCount()
        {
            return DBHelper.ExecuteIntScalar(string.Format("SELECT count(*) FROM AlbumTB"));
        }

        public static List<AlbumEntity> GetParentAlbumlList(int parentId)
        {
            List<AlbumEntity> list = new List<AlbumEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM AlbumTB where ParentId={0}", parentId)))
            {
                return FillData(dr);
            }
        }

        /// <summary>
        /// 获取信息实体
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static AlbumEntity GetAlbumEntity(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM AlbumTB WHERE {0}", condition)))
            {
                List<AlbumEntity> list = FillData(dr);
                return list == null || list.Count == 0 ? (new AlbumEntity()) : list[0];
            }
        }

        /// <summary>
        /// 根据标识获取信息实体重载一
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static AlbumEntity GetAlbumEntity(int albumId)
        {
            return GetAlbumEntity(string.Format("AlbumId = {0}", albumId));
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="te"></param>
        /// <returns></returns>
        public static bool SaveAlbumInfor(AlbumEntity te)
        {
            int i = 0;
            if (te.AlbumId <= 0)
            {
                i = DBHelper.ExecuteNonQuery(string.Format("insert into AlbumTB (AlbumName,CoverPicUrl,[Describe],CrtTime,ParentId) VALUES('{0}','{1}','{2}','{3}',{4})", te.AlbumName, te.CoverPicUrl, te.Describe, te.CrtTime, te.PAlbumInfo.AlbumId));
            }
            else
            {
                i = DBHelper.ExecuteNonQuery(string.Format("UPDATE AlbumTB SET AlbumName ='{0}' ,CoverPicUrl ='{1}',[Describe]='{2}',CrtTime='{3}',ParentId={4} where AlbumId={5}", te.AlbumName, te.CoverPicUrl, te.Describe, te.CrtTime, te.PAlbumInfo.AlbumId, te.AlbumId));
            }
            return i > 0 ? true : false;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DeleteAlbumInfor(int albumId)
        {
            return DBHelper.ExecuteNonQuery(string.Format("DELETE FROM AlbumTB WHERE AlbumId = {0}", albumId)) >= 0 ? true : false;
        }
        #endregion

        #region 私有方法

        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static List<AlbumEntity> FillData(IDataReader dr)
        {
            List<AlbumEntity> list = new List<AlbumEntity>();
            AlbumEntity te;
            while (dr != null && dr.Read())
            {
                te = new AlbumEntity();
                te.AlbumId = ConvertHelper.IntParse(dr["AlbumId"]);
                te.AlbumName = ConvertHelper.StringParse(dr["AlbumName"]);
                te.CoverPicUrl = ConvertHelper.StringParse(dr["CoverPicUrl"]);
                te.Describe = ConvertHelper.StringParse(dr["Describe"]);
                te.CrtTime = ConvertHelper.DateTimeParse(dr["CrtTime"]);

                te.PAlbumInfo.AlbumId = ConvertHelper.IntParse(dr["ParentId"]);

                list.Add(te);
            }
            return list;
        }

        private static List<AlbumEntity> DeepFillData(IDataReader dr)
        {
            List<AlbumEntity> list = new List<AlbumEntity>();
            AlbumEntity te;
            while (dr != null && dr.Read())
            {
                te = new AlbumEntity();
                te.AlbumId = ConvertHelper.IntParse(dr["AlbumId"]);
                te.AlbumName = ConvertHelper.StringParse(dr["AlbumName"]);
                te.CoverPicUrl = ConvertHelper.StringParse(dr["CoverPicUrl"]);
                te.Describe = ConvertHelper.StringParse(dr["Describe"]);
                te.CrtTime = ConvertHelper.DateTimeParse(dr["CrtTime"]);

                te.PAlbumInfo.AlbumId = ConvertHelper.IntParse(dr["ParentId"]);
                te.PAlbumInfo.AlbumName = ConvertHelper.StringParse(dr["PAlbumName"]);

                list.Add(te);
            }
            return list;
        }

        #endregion
    }
}
